<template>
  <div :style="{height:'300px',width:'100%'}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  name: "AddressChart",
  mixins: [resize],
  data() {
    return {
      chart: null,
      addressArr: [], // 地区
      addressCount: [] // 地区人数
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
    }
  },
  props: ["studentData"],
  created() {
    setTimeout(() => {
      var addressData = [];
      var sum = {};
      for (var i = 0; i < this.studentData.length; i++) {
        var address = this.studentData[i].address;
        if (toCom(address)) {
          addressData.push({ address: address });
          sum[address + "Sum"] = 1;
        } else {
          sum[address + "Sum"] += 1;
        }
        function toCom(address) {
          for (var i = 0; i < addressData.length; i++) {
            if (addressData[i].address === address) {
              return false;
            }
          }
          return true;
        }
      }
      for (var i in sum) {
        this.addressCount.push(sum[i]);
      }
      for (var i = 0; i < addressData.length; i++) {
        var man = addressData[i].address;
        this.addressArr.push(man);
      }
      // 开始绘图数据
      this.chart.setOption({
        color: ["#87cefa"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        xAxis: [
          {
            type: "category",
            data: this.addressArr,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: "value"
          }
        ],
        series: [
          {
            name: "人数",
            type: "bar",
            barWidth: "30%",
            data: this.addressCount
          }
        ]
      });
    }, 900);
  }
};
</script>